package org.ohos.rajawali3d.examples;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import org.ohos.rajawali3d.examples.data.RajawaBean;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;

public class ExamplesAbility extends Ability {

    public static final String EXTRA_EXAMPLE_URL = "EXTRA_EXAMPLE_URL";
    public static final String EXTRA_EXAMPLE_CLASS = "EXTRA_EXAMPLE_CLASS";
    public static final String EXTRA_EXAMPLE_TYPE = "EXTRA_EXAMPLE_TYPE";
    public static final String EXTRA_EXAMPLE_NAME = "EXTRA_EXAMPLE_NAME";
    public static final String EXTRA_EXAMPLE_PATH = "EXTRA_EXAMPLE_PATH";

    @Override
    protected void onStart(Intent savedInstanceState) {
        super.onStart(savedInstanceState);
        //super.setMainRoute(ExamplesSlice.class.getName());
        if (savedInstanceState != null) {
            IntentParams extras = savedInstanceState.getParams();
            if (extras != null) {
                processExtras(extras);
            }
        }
        setWindowBackgroundColor(0xFF, 0, 0);
    }

    private void processExtras(IntentParams extras) {
        String url = (String) extras.getParam(ExamplesAbility.EXTRA_EXAMPLE_URL);
        String class_name = (String) extras.getParam(ExamplesAbility.EXTRA_EXAMPLE_CLASS);
        int type = (int) extras.getParam(ExamplesAbility.EXTRA_EXAMPLE_TYPE);
        int name = (int) extras.getParam(ExamplesAbility.EXTRA_EXAMPLE_NAME);
        String path = (String) extras.getParam(ExamplesAbility.EXTRA_EXAMPLE_PATH);
        Class aCl;
        try {
            aCl = Class.forName(class_name);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        RajawaBean example = new RajawaBean(name, type, aCl, url, path);

        Class aClass = example.getExampleClass();
        try {
            AExampleFragment fragment = (AExampleFragment) aClass.newInstance();
            setMainRoute(fragment.getClass().getName());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
}
